package com.yyw.foodie.controller.base;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yyw.foodie.bo.address.AddressBo;
import com.yyw.foodie.common.Result;
import com.yyw.foodie.enums.YesOrNoEnums;
import com.yyw.foodie.pojo.UserAddressEntity;
import com.yyw.foodie.service.UserAddressService;
import com.yyw.foodie.utils.BindingResultJoinError;
import com.yyw.foodie.utils.RegexUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户地址表  前端控制器
 * </p>
 *
 * @author yiyw
 * @since 2021-05-22
 */
@Slf4j
@RestController
@RequestMapping("/address")
@Api(value = "用户收货地址相关", tags = "用户收货地址相关维护")
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @PostMapping("/add")
    @ApiOperation(value = "新增收货地址", notes = "新增用户收货地址")
    public Result addAddress(@RequestBody @Valid AddressBo addressBo, BindingResult bindingResult) {
        // 0. 校验数据完整性
        Map<String, String> map = BindingResultJoinError.joinErrorMsg(bindingResult);
        if (null != map) {
            return Result.errorMap(map);
        }
        if (!RegexUtils.validateMobilePhone(addressBo.getMobile())) {
            return Result.errorMsg("手机号输入有误");
        }
        // 1. 新增地址
        UserAddressEntity address = new UserAddressEntity();
        BeanUtils.copyProperties(addressBo, address);
        userAddressService.save(address);
        return Result.ok();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改收货地址", notes = "修改用户收货地址")
    public Result updateAddress(@RequestBody @Valid AddressBo addressBo, BindingResult bindingResult) {
        // 0. 校验数据完整性
        Map<String, String> map = BindingResultJoinError.joinErrorMsg(bindingResult);
        if (null != map) {
            return Result.errorMap(map);
        }
        if (StringUtils.isBlank(addressBo.getAddressId())) {
            return Result.errorMsg("地址ID为空");
        }
        if (!RegexUtils.validateMobilePhone(addressBo.getMobile())) {
            return Result.errorMsg("手机号输入有误");
        }

        // 1. 修改地址
        UserAddressEntity address = new UserAddressEntity();
        BeanUtils.copyProperties(addressBo, address);
        address.setId(addressBo.getAddressId());
        boolean update = userAddressService.updateById(address);
        return update ? Result.ok() : Result.errorMsg("修改失败,未找到数据");
    }


    @PostMapping("/delete")
    @ApiOperation(value = "用户删除地址", notes = "用户删除地址")
    public Result delAddress(@ApiParam(value = "用户ID") @RequestParam(value = "userId") String userId,
                             @ApiParam(value = "地址ID") @RequestParam(value = "addressId") String addressId) {
        // 0. 校验数据完整性
        if (StringUtils.isBlank(userId)) {
            log.error("请输入用户名ID");
            return Result.errorMsg("请输入用户名ID");
        }
        if (StringUtils.isBlank(addressId)) {
            log.error("请输入地址ID");
            return Result.errorMsg("请输入地址ID");
        }
        // 1. 删除地址
        QueryWrapper<UserAddressEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserAddressEntity::getId, addressId).eq(UserAddressEntity::getUserId, userId);
        boolean remove = userAddressService.remove(queryWrapper);
        return remove ? Result.ok() : Result.errorMsg("删除失败,未找到数据");
    }


    @PostMapping("/setDefalut")
    @ApiOperation(value = "用户设置默认地址", notes = "用户设置默认地址")
    public Result setDefaultAddress(@ApiParam(value = "用户ID") @RequestParam(value = "userId") String userId,
                                    @ApiParam(value = "地址ID") @RequestParam(value = "addressId") String addressId) {
        // 0. 校验数据完整性
        if (StringUtils.isBlank(userId)) {
            log.error("请输入用户名ID");
            return Result.errorMsg("请输入用户名ID");
        }
        if (StringUtils.isBlank(addressId)) {
            log.error("请输入地址ID");
            return Result.errorMsg("请输入地址ID");
        }
        // 1. 修改地址
        UpdateWrapper<UserAddressEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(UserAddressEntity::getIsDefault, YesOrNoEnums.是.code)
                .eq(UserAddressEntity::getId, addressId).eq(UserAddressEntity::getUserId, userId);
        boolean update = userAddressService.update(updateWrapper);
        return update ? Result.ok() : Result.errorMsg("修改失败,未找到数据");
    }


    @PostMapping("/list")
    @ApiOperation(value = "根据用户id查询收货地址列表", notes = "根据用户id查询收货地址列表")
    public Result listByUserId(@ApiParam(value = "用户ID") @RequestParam(value = "userId") String userId) {
        // 0. 校验数据完整性
        if (StringUtils.isBlank(userId)) {
            log.error("请输入用户名ID");
            return Result.errorMsg("请输入用户名ID");
        }
        // 1. 查询数据
        QueryWrapper<UserAddressEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserAddressEntity::getUserId, userId).orderByDesc(UserAddressEntity::getCreatedTime);
        List<UserAddressEntity> list = userAddressService.list(queryWrapper);
        return Result.ok(list);
    }

}

